package com.yanyi.basepermission;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.benyanyi.permissionlib.callback.PermissionCallBack;
import com.benyanyi.permissionlib.PermissionHelper;
import com.benyanyi.permissionlib.type.PermissionType;

/**
 * @author myLove
 * @date 2018-06-06 11:30
 * @email ben@yanyi.red
 * @overview
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);
        PermissionHelper.with(this)
                .setPermissions(Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.MANAGE_EXTERNAL_STORAGE,
                        PermissionType.SHORTCUT)
                .setDefaultPermissionDialogInfo()
                .request(new PermissionCallBack() {
                    @Override
                    public void onSuccess() {
                        log("请求成功");
                    }

                    @Override
                    public void onFailure(String[] failurePermissions) {
                        log("请求失败");
                        for (String failurePermission : failurePermissions) {
                            log(failurePermission);
                        }
                    }

                    @Override
                    public void onComplete() {
                        log("请求结束");
                    }
                });

//                .onPermissionSuccess(() -> log("请求成功"))
//                .onPermissionFailure(strings -> {
//                    StringBuilder str = new StringBuilder();
//                    for (String s : strings) {
//                        str.append("\n").append(s);
//                    }
//                    log(str.toString());
//                })
//                .onPermissionComplete(() -> log("请求完成"));
    }

    private Intent getAppDetailSettingIntent(Activity activity) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", activity.getApplicationContext().getPackageName(), null));
        } else {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", activity.getApplicationContext().getPackageName());
        }
        return localIntent;
    }

    private void log(Object object) {
        Log.d(defaultTag(), object.toString());
    }

    private String defaultTag() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        StackTraceElement log = stackTrace[1];
        String tag = null;
        for (int i = 1; i < stackTrace.length; i++) {
            StackTraceElement e = stackTrace[i];
            if (!e.getClassName().equals(log.getClassName())
                    && !"defaultTag".equals(e.getMethodName())
                    && !"log".equals(e.getMethodName())) {
                tag = e.getClassName() + "." + e.getMethodName();
                break;
            }
        }
        if (tag == null) {
            tag = log.getClassName() + "." + log.getMethodName();

        }
        return tag;
    }
}
